<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;
use app\common\model\GoodsCategory;
class GoodsDonateFee extends Model
{

    use SoftDelete;

    

    // 表名
//    protected $name = 'GoodsDonate';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
    ];

    public function getThumbnailImageAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['thumbnailimage']) ? $data['thumbnailimage'] : '');
        return get_app_image_url($value);
    }
    public function getGoodsContentAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['goodscontent']) ? $data['goodscontent'] : '');
        return imageUrl($value);
    }
    public function getThumbnailImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['thumbnailimages']) ? $data['thumbnailimages'] : '');
        $value=explode(',',$value);
        foreach ($value as $k=>&$v){
            $v=get_app_image_url($v);
        }
        return $value;
    }
    public function getImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        $value=explode(',',$value);
        foreach ($value as $k=>&$v){
            $v=get_app_image_url($v);
        }
        return $value;
    }
    public function category1()
    {
        return $this->belongsTo('GoodsCategory', 'cid1', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function category2(){
        return $this->belongsTo('GoodsCategory', 'cid2', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function category3(){
        return $this->belongsTo('GoodsCategory', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function admin()
    {
        return $this->hasOne('ShopApply', 'admin_id', 'shop_id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user(){
        return $this->belongsTo('User', 'uid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function freight(){
        return $this->belongsTo('goodsDonateFee', 'freight_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 商品详情
     * */
    public static function getInfo($id){
        $where['goods_donate.id'] = $id;
        $info  = self::where($where)
            ->with(['category1','user','freight'])
            ->find();
        if(!$info) return false;
        $info->getRelation('user')->visible(['avatar','nickname','id']);
        $info->getRelation('category1')->visible(['id','name']);
        $info->getRelation('freight')->visible(['id','weight']);
        $info['cname'] = $info['category1']['name'];
        $info['user_avatar'] = $info['user']['avatar'];
        $info['user_nickname'] = $info['user']['nickname'];
        $info['user_id'] = $info['user']['id'];
        $set = get_set();
        $info->send =$set['conversion'];
        return $info;
    }

}
